﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Xml.Linq;
using DO.Workflows;

namespace BLL.Workflows
{
    public abstract class WorkerBase : IWorker
    {
        private Thread _WorkerThread;
        private Guid _JobTicketID;
        private RunContext _Context;
        private string _WorkflowName;
        private string _ComponentName;

        #region Implementation of IWorker
        public string ComponentName
        {
            get { return _ComponentName; }
        }

        public string WorkflowName
        {
            get { return _WorkflowName; }
        }

        /// <summary>
        /// handler to worker thread
        /// </summary>
        public Thread WorkerThread
        {
            get { return _WorkerThread; }
        }

        /// <summary>
        /// allow instruction to be changed at runtime
        /// </summary>
        public abstract InstructionBase Instruction { get; set; }
        
        /// <summary>
        /// output
        /// </summary>
        public abstract ResultBase Output{ get;}
        
        public Guid JobTicket
        {
            get
            {
                return this._JobTicketID;
            }
            protected set
            {
                this._JobTicketID = value;
            }
        }

        

        /// <summary>
        /// allow worker to be created based on component type
        /// </summary>
        public abstract Type ComponentType
        {
            get;
        }

        /// <summary>
        /// allow instruction to be instantiated
        /// </summary>
        public abstract Type InstructionType { get;}

        /// <summary>
        /// runtime context retrieved from appl domain
        /// </summary>
        public RunContext Context
        {
            get { return _Context; }
            set { _Context = value; }
        }

        /// <summary>
        /// this must be called before actual work
        /// </summary>
        /// <param name="ticketGuid"></param>
        /// <param name="instructionXml"></param>
        /// <param name="workflowName"></param>
        /// <param name="componentName"></param>
        public void SetInstruction(Guid ticketGuid, string instructionXml, string workflowName,
                                            string componentName)
        {
            XElement xe = XElement.Parse(instructionXml);
            this._JobTicketID = ticketGuid;
            InstructionBase instruction = (InstructionBase) Activator.CreateInstance(this.InstructionType);
            this.Instruction = instruction.Instantiate(xe);
            this._WorkflowName = workflowName;
            this._ComponentName = componentName;
        }

        /// <summary>
        /// this is called within subflow, where runtime context is also initialized
        /// </summary>
        /// <param name="context"></param>
        /// <param name="ticketGuid"></param>
        /// <param name="request"></param>
        /// <param name="workflowName"></param>
        /// <param name="componentName"></param>
        public void SetInstruction(RunContext context, Guid ticketGuid, InstructionBase request,
                                            string workflowName, string componentName)
        {
            this._JobTicketID = ticketGuid;
            this.Instruction = request;
            this._Context = context;
            this._WorkflowName = workflowName;
            this._ComponentName = componentName;
        }

        /// <summary>
        /// this is called synchronously, used in testing mode
        /// </summary>
        public void DoWork()
        {
            if (this.WorkerStartedEvent != null)
            {
                this.WorkerStartedEvent(this._JobTicketID, this, this.GetType(), this.Instruction);
            }
            this._WorkerThread = Thread.CurrentThread;
            this.StartWork();
        }

        /// <summary>
        /// this is called by job dispatcher
        /// </summary>
        public void DoWorkAsync()
        {
            if (this.WorkerStartedEvent != null)
            {
                this.WorkerStartedEvent(this._JobTicketID, this, this.GetType(), this.Instruction);
            }
            this._WorkerThread = new Thread(new ThreadStart(this.StartWork));
            this._WorkerThread.Start();
        }

        /// <summary>
        /// this is called by job monitor/manager
        /// </summary>
        public void CancelWork()
        {
            if (this._WorkerThread != null && this._WorkerThread.IsAlive)
            {
                this._WorkerThread.Abort();
                if (this.WorkerCancelledEvent != null)
                {
                    this.WorkerCancelledEvent(this._JobTicketID, this.GetType(), this.Instruction);
                }
            }
        }

        public event WorkerStarted WorkerStartedEvent;
        public abstract event WorkerFinished WorkerFinishedEvent;
        public abstract event WorkerHasError WorkerErredEvent;
        public event WorkerCancelled WorkerCancelledEvent;
        public abstract event WorkerStatusChanged WorkerStatusChangedEvent;

        #endregion

        #region actual work
        protected abstract void StartWork();
        #endregion


    }
}
